package cn.lutu.lutu.controller;

import cn.lutu.lutu.response.ResultVO;
import cn.lutu.lutu.service.EmailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
@Api(tags = "邮箱模块")
@RequestMapping("/v1/emails/")
public class EmailController {

    @Autowired
    EmailService emailService;
    @Autowired(required = false)
    JavaMailSender javaMailSender;

    @PostMapping("/{email}/code")
    @ApiOperation("1.发送注册验证码")
    public ResultVO sendEmailCode(@PathVariable String email){
        /************** 此处需加入邮箱格式验证***************/
        log.debug("邮箱名为："+email);
        Integer emailCode=emailService.randomEmailCode();
        SimpleMailMessage codeEmail = emailService.createCodeEmail(email,emailCode);
        log.debug("邮箱内容为："+codeEmail.getText());
        javaMailSender.send(codeEmail);
        /*传回EmailCode到前端进行匹配验证*/
        return ResultVO.ok(emailCode);
    }
}
